from gym.models import Activity
from django.template import RequestContext
from core.models import Company, NaturalPerson
from django.shortcuts import render_to_response
from academia.views_helper import build_menu, paginator
from django.contrib.auth.decorators import login_required, permission_required
from core.forms import ActivityListForm, NaturalPersonForm, UserForm, AddressForm, ContactForm


@login_required
@permission_required('list_activity')
def activities(request):
    userMainMenus = build_menu(request)
    user = request.user

    activities = []
    if user.is_superuser and not user.contract:
        activities = Activity.objects.values_list('id', 'name', 'price').filter(active=True)

    elif user.is_superuser and user.contract:
        company = Company.objects.get(user=user)
        if company:
            activities = Activity.objects.values_list('id', 'name', 'price').filter(gym=company, active=True)
        else:
            person = NaturalPerson.objects.filter(user=user)
            activities = Activity.objects.values_list('id', 'name', 'price').filter(gym=person.gym, active=True)
    elif not user.is_superuser and user.contract:
        person = NaturalPerson.objects.filter(user=user)
        activities = Activity.objects.values_list('id', 'name', 'price').filter(gym=person.gym, active=True)

    form = ActivityListForm(objects=activities)

    page = request.GET.get('page')

    paginator(form, page)

    return render_to_response('dashboard/conteudo.html', locals(), context_instance=RequestContext(request))

@login_required
@permission_required('add_activity')
def activity(request):
    pass

@login_required
@permission_required('delete_activity')
def activity_delete(request, activity):
    pass

@login_required
@permission_required('change_activity')
def activity_edit(request, activity):
    pass

@login_required
@permission_required('list_student')
def students(request):
    pass

@login_required
@permission_required('add_student')
def student(request):
    userMainMenus = build_menu(request)

    is_post = request.method == 'POST'

    form = NaturalPersonForm(request.POST, request.FILES, current_user=request.user) if is_post else NaturalPersonForm(
        current_user=request.user)
    user_form = UserForm(request.POST, user=request.user) if is_post else UserForm(user=request.user)
    address_form = AddressForm(request.POST) if is_post else AddressForm()
    contact_form = ContactForm(request.POST) if is_post else ContactForm()

    if is_post:
        if form.is_valid():
            form.save()
            success = True

            form = NaturalPersonForm(current_user=request.user)
            user_form = UserForm(user=request.user)
            address_form = AddressForm()
            contact_form = ContactForm()

    includes = {
        'address': {'form': address_form, 'template': 'dashboard/address/address.html'},
        'contact': {'form': contact_form, 'template': 'dashboard/contact/contact.html'},
        'user': {'form': user_form, 'template': 'dashboard/user/user.html'},
    }

    return render_to_response('dashboard/form.html', locals(), context_instance=RequestContext(request))

@login_required
@permission_required('delete_student')
def student_delete(request, student):
    pass

@login_required
@permission_required('change_student')
def student_edit(request, student):
    pass

@login_required
@permission_required('list_employee')
def employees(request):
    pass

@login_required
@permission_required('add_employee')
def employee(request):
    pass

@login_required
@permission_required('delete_employee')
def employee_delete(request, employee):
    pass

@login_required
@permission_required('change_employee')
def employee_edit(request, employee):
    pass

@login_required
@permission_required('list_teacher')
def teachers(request):
    pass

@login_required
@permission_required('add_teacher')
def teacher(request):
    pass

@login_required
@permission_required('delete_teacher')
def teacher_delete(request, teacher):
    pass

@login_required
@permission_required('change_teacher')
def teacher_edit(request, teacher):
    pass

@login_required
@permission_required('list_registrationformmodel')
def registrations_form_model(request):
    pass

@login_required
@permission_required('add_registrationformmodel')
def registration_form_model(request):
    pass

@login_required
@permission_required('delete_registrationformmodel')
def registration_form_model_delete(request, registration_form_model):
    pass

@login_required
@permission_required('change_registrationformmodel')
def registration_form_model_edit(request, registration_form_model):
    pass
